$(function () {
    $('.datetimepicker').datetimepicker({
        icons: {
            time: "fa fa-clock-o",
            date: "fa fa-calendar",
            up: "fa fa-chevron-up",
            down: "fa fa-chevron-down",
            previous: 'fa fa-chevron-left',
            next: 'fa fa-chevron-right',
            today: 'fa fa-screenshot',
            clear: 'fa fa-trash',
            close: 'fa fa-remove'
        },
        format: 'YYYY-MM-DD',
    });
})

$('#sta_time').val(dateToYMD(new Date()));
$('#end_time').val(dateToYMD(new Date()));

var data_source;
var page_count = 15;

findAllEvent();

/**
 * 点击搜索
 */
function searchAction() {
    findAllEvent();
}
/**
 * 查询比赛列表
 */
function findAllEvent() {

    var startTime = $('#sta_time').val();
    var endTime = $('#end_time').val();
    var status = $('#status_select').val();
    var keyword = $('#keyword').val();

    var data = {};
    if (null == startTime){
        showNotification('bottom', 'right', '请选择开始日期');
        return;
    }
    if (null == endTime){
        showNotification('bottom', 'right', '请选择结束日期');
        return;
    }

    if (endTime < startTime){
        showNotification('bottom', 'right', '日期格式不正确');
        return;
    }

    data['date'] = startTime;
    data['endDate'] = endTime;

    if (null != status){
        data['state'] = status;
    }
    if (null != keyword && keyword.length != 0){
        data['keyword'] = keyword;
    }

    $.ajax({
        url: baseUrl.base + '/country/eventList',
        type: 'get',
        data: data,
        success: function (data) {
            if (data.code == 0) {
                var res = data.results;

                data_source = res;

                cutPagination(1);

                // 配置分页
                $("#p1").pagination({
                    totalData: res.length,
                    showData:page_count,
                    callback: function (idx) {
                        cutPagination(idx);
                    }
                });
            }else {
                showNotification('bottom','right',data.msg);
            }
        }
    });
}

/**
 * 保存按钮
 * @param element
 */
var changeEventMap = {};

function saveAction(element) {
    var events = new Array();

    var list = document.getElementsByName("checkbox");
    for (var i = 0;i < list.length;i ++){
        var ch = list[i];

        var eventId = ch.getAttribute("eventId");
        var homeTeamName = ch.getAttribute("homeTeamName");
        var awayTeamName = ch.getAttribute("awayTeamName");
        var eventTime = ch.getAttribute("eventTime");
        var isHot = ch.checked == true ? 1 : 0;

        var event = {
            'eventId':eventId,
            'isHot':isHot,
            'homeName':homeTeamName,
            'awayName':awayTeamName,
            'eventTime':eventTime,
        }
        events.push(event);
    }

    var data = {
        'eventList':JSON.stringify(events)
    }

    $.ajax({
        url: baseUrl.base + '/customEvent/setEvents',
        type: 'post',
        data: data,
        success: function (data) {
            if (data.code == 0){
                showNotification('bottom', 'right', '成功');
            }else {
                showNotification('bottom', 'right', '失败');
            }
        }
    });
}

/**
 * 选择框事件
 * @param element
 */
function changeCheckd(element) {
    var ch = document.getElementById("header_check");
    ch.checked = false;

}

/**
 * 全选和反选
 * @param element
 */
function headerChecked(element) {

    var list = document.getElementsByName("checkbox");

    var checked = element.checked;

    for (var i = 0;i < list.length;i ++){
        var ch = list[i];
        ch.checked = checked;
    }
}

/**
 * 获取状态字符串
 * @param status
 */
function statusStr(status) {

    var str = '未知';
    if (status == 0) {
        str = "未开赛";
    } else if (status == 1) {
        str = "已结束";
    } else if (status == 2) {
        str = "数据取消";
    } else if (status == 3) {
        str = "正在进行";
    } else if (status == 4) {
        str = "延期";
    } else if (status == 5) {
        str = "中途废弃";
    }
    return str;
}

/**
 * 分页
 * @param page 从1开始
 */
function cutPagination(page) {

    var start = (page - 1) * page_count;
    var end = start + page_count;

    if (end > data_source.length){
        end = data_source.length;
    }

    $("tbody").html("");
    for (var i = start; i < end; i++) {
        var event = data_source[i];

        var eventId = event.eventId;
        var eventName = event.homeTeamName + 'VS' + event.awayTeamName;
        var competitionName = event.competitionName;
        var time = timestampToTime(event.eventTime / 10000);
        var isHot = event.isHot;

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var div = document.createElement("div");
        div.setAttribute("class","form-check");
        div.setAttribute("style","padding-top: 1rem");

        var label = document.createElement("label");
        label.setAttribute("class","form-check-label");

        var input = document.createElement("input");
        input.setAttribute("class","form-check-input");
        input.setAttribute("name","checkbox");
        input.setAttribute("type","checkbox");
        input.setAttribute("eventId",eventId);
        input.setAttribute("homeTeamName",event.homeTeamName);
        input.setAttribute("awayTeamName",event.awayTeamName);
        input.setAttribute("eventTime",time);
        input.onchange = function (ev) {
            changeCheckd(this);
        }

        if (isHot == 1){
            input.checked = true;
        }

        label.appendChild(input);

        var span1 = document.createElement("span");
        span1.setAttribute("class","form-check-sign");
        label.appendChild(span1);

        var span2 = document.createElement("span");
        span2.setAttribute("class","check");
        span1.appendChild(span2);

        div.appendChild(label);
        td.appendChild(div);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(eventId);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(eventName);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(competitionName);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(time);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(statusStr(event.state));
        td.appendChild(tdContent);
        tr.appendChild(td);

        document.getElementById("tbody").append(tr);
        document.getElementById("tbody").innerHTML;
    }

}